define(function () {
    'use strict';

    function ctrl($scope,$q,InfoService,HistoryNavChartService,$ionicLoading, $stateParams, CommonService) {

        $scope.channel = {};

        $scope.goldChartConfig = HistoryNavChartService.historyIndexChart(null);

        $scope.$on('$ionicView.beforeEnter', function() {
            init();
        });

        $scope.doRefresh = function() {
            init().finally(function(){
                $scope.$broadcast('scroll.refreshComplete');
            });
        };

        function init(){
            if(!$scope.funds){
                $ionicLoading.show();
            }
            $scope.channel.channelId = $stateParams['id'];
            return $q.all([queryIndexProfit(), queryHistoryIndex(), queryContentBriefs()]);
        }


        //查询指数收益率
        function queryIndexProfit(){
            var p = InfoService.getIndexProfit($scope.channel.title);
            p.then(function(data){
                if(data){
                    $scope.gold = data[0];
                }
            });
            return p;
        }

        //绘制走势图
        function queryHistoryIndex(){
            var p = InfoService.getHistoryIndex($scope.channel.title, 60);
            p.then(function(data){
                if(data){
                    //画收盘走势图
                    $scope.goldChartConfig = HistoryNavChartService.historyIndexChart(data);
                    $scope.data_date = CommonService.formatDate(data[data.length - 1].data_date);
                }
            });
            return p;
        }

        //查询基金概况
        function queryContentBriefs(){
            var p = InfoService.getProductsByChannelId($scope.channel.channelId);
            p.then(function(data){
                if(data){
                    $scope.funds = data;
                    for(var i=0;i<$scope.funds.length;i++){
                        var fund = $scope.funds[i];
                        if(fund.prod_min_subscribe){
                            fund.prod_min_subscribe = CommonService.getMinAmount(fund);
                        }
                    }
                }
            });
            $ionicLoading.hide();
            return p;
        }

    }

    ctrl.$inject = ['$scope','$q','InfoService','HistoryNavChartService','$ionicLoading', '$stateParams', 'CommonService'];
    return ctrl;
});